
use std::pin::Pin;
use std::sync::Arc;
use tool::tooll_drop::Defer;
use mysql::mysql_helper::{MysqlHelper,MysqlAccount};
use tool::{tool_image, tool_io, tool_result::Error};
use tool::tool_convert;

pub async fn test1() -> Result<(), Error> {
    let mut zip_control= tool_io::ZipControl::new(Box::new(|all, cur, stop| {
        Box::pin(async move {
            println!("all:{}  cur:{}",all,cur);
            if cur as f64/all as f64>0.5{
                {
                    let mut stop_write= stop.write().await;
                    *stop_write=true;
                }
            }
        })
    }));
    tool_io::ZipHelper::zip_dir("./backup/伊哈赛事数据库/2024-09-27", "./test.zip",&mut zip_control).await?;

    Ok(())
}

pub async fn test2() -> Result<(), Error> {
    let img_str= tool_image::ImageHelper::convert_image_to_ascii("./assets/images/guoba.png", 90, 30)?;
    println!("{}",img_str);
    Ok(())
}

pub async fn test3() -> Result<(), Error> {
    let sql=MysqlHelper::new(MysqlAccount::new(1, "127.0.0.1:3306".to_string(), "".to_string(), "".to_string(), "".to_string())).await?;
    Defer::new(Box::new(move || {
        sql.close();
    }));
    Ok(())
}

// pub async fn test4()->Result<(), Error>{
//     let a=1;
//     let a_ptr= std::ptr::addr_of!(a);
//     println!("{:p}",a_ptr);//0x1db2d7ebc4
//     let mut b=Box::pin(a);
//     let mut b_ptr= std::ptr::addr_of!(b);
//     println!("{:p}",b_ptr);//0x1db2d7ec20
//     b=Box::pin(2);//这里为什么没报错，我已经给b重新赋值了
//     b_ptr= std::ptr::addr_of!(b);
//     println!("{:p}",b_ptr);//0x1db2d7ec20
//     Ok(())
// }


pub async fn test4()->Result<(), Error>{
    println!("------不用pin------");
    let a="1".to_string();
    let a_ptr= std::ptr::addr_of!(*a);
    println!("{:p}",a_ptr);
    let mut b=a;
    let mut b_ptr= std::ptr::addr_of!(*b);
    println!("{:p}",b_ptr);
    b="2".to_string();
    b_ptr= std::ptr::addr_of!(*b);
    println!("{:p}",b_ptr);
    test5().await?;
    Ok(())
}

pub async fn test5()->Result<(), Error>{
    println!("------用pin------");
    let a="1".to_string();
    let a_ptr= std::ptr::addr_of!(*a);//0x27973efd860
    println!("{:p}",a_ptr);
    let mut b=Box::pin(a);
    let mut b_ptr= std::ptr::addr_of!(*b);//0x27973f346c0
    println!("{:p}",b_ptr);
    b=Box::pin("2".to_string());
    b_ptr= std::ptr::addr_of!(*b);//0x27973f34660
    println!("{:p}",b_ptr);
    test6().await?;
    Ok(())
}

pub async fn test6()->Result<(), Error>{
    let a = Box::new("Hello".to_string());
    let mut pinned = Pin::new(a);  // Pin会防止a在堆上的地址被移动

    // 这行代码会发生编译错误，因为Pin会阻止数据在内存中移动
    let b = pinned;  // 移动了Pin包裹的数据

    println!("--end--");

    Ok(())
}